<?php
namespace common\components;

use Yii;
use yii\web\Controller;

class BaseController extends Controller
{
	public function init()
    {
        $this->setLanguage();
    }

    /**
     * 设置语言显示
     */
    public function setLanguage()
    {
        if(isset($_GET['lang']) && $_GET['lang'] != "") {

            Yii::$app->language = htmlspecialchars($_GET['lang']);
            $cookies = Yii::$app->response->cookies;
            // 将语言类型保存在cookie
            $cookies->add(new \yii\web\Cookie([
                'name' => 'lang',
                'value' => htmlspecialchars($_GET['lang']),
                'expire' => time() + (365 * 24 * 60 * 60),
            ]));
        } elseif (isset(Yii::$app->request->cookies['lang']) && 
            Yii::$app->request->cookies['lang']->value != "") {
            //cookie的语言设置
            Yii::$app->language = Yii::$app->request->cookies['lang']->value;
        } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            // 根据浏览器设置语言
            $lang = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
            Yii::$app->language = $lang[0];
        } else {
            Yii::$app->language = 'zh-CN';
        }
    }
    protected function renderJson($params = array())
    {
        yii::$app->response->format = Response::FORMAT_JSON;
        return $params;
    }

    protected function getAllParams() {
        return array_merge(Yii::$app->request->post(), Yii::$app->request->get());
    }
    /**
     * @param $message
     * @return array
     */
    protected function verifySms($result)
    {
        $params = $this->getAllParams();
        $flag = array_keys($params)[0];

        $newMcode = $params[$flag]['mcode'];
        $mcode = Yii::$app->session->get('mcode');

        if ($mcode != $newMcode) {
            return $result;
        }

    }
}
